/**
 * 文章列表js
 * date: 2010-12-9
 */
COMM.lib.load("form");
COMM.lib.load("pager");
COMM.lib.load("dialog");
(function($){

	var tableBody;
	
	var Parameter = {
		"rp.categoryType" : -1,
		"rp.artcletype" : -1,
		"rp.status" : -1,
		"rp.pageNo" : 1
	};
	//由于目前使用的对话框没有使用jquery
	//如果往对话框中添加js脚步，这些js不会被执行，so需要手动执行
	function executeScript(html) {
		if($.browser.mozilla) return;
	    var reg = /<script[^>]*>([^\x00]+)$/i;
	    //对整段HTML片段按<\/script>拆分
	    var htmlBlock = html.split("<\/script>");
	    for (var i in htmlBlock) {
	        var blocks;//匹配正则表达式的内容数组，blocks[1]就是真正的一段脚本内容，因为前面reg定义我们用了括号进行了捕获分组
	        if (blocks = htmlBlock[i].match(reg)) {
	            //清除可能存在的注释标记，对于注释结尾-->可以忽略处理，eval一样能正常工作
	            var code = blocks[1].replace(/<!--/, '');
	            try {
	                eval(code); //执行脚本
	            } catch (e) {
	            }
	        }
	    }
	}
	var articleForm;
	var Operation = {
		modify : function(article){
			if(!articleForm){
				$.get("/admin/page/article/articleInput.html",function(data){
					articleForm = data;
					Operation.makeModifyDialog(article);
				});
			} else {
				Operation.makeModifyDialog(article);
			}
			
		},
		makeModifyDialog : function(article){
			$.dialog({
				title:'文章修改',
			    content: articleForm,
			    yesFn : function(){
					$.postForm({
						formId : "#articleForm",
						url: "/admin/article/edit.do",
						success: function(data){
							if(data.result == 0){
								alert("修改失败！");
							} else {
								alert("修改成功！");
								ArticleList.getData();
							}
						}
					});
				},
				initFn : function(){
					executeScript(articleForm);
					setTimeout(function(){
						Article.setValue(article);
					}, 200);
				}
			});
		},
		del : function(article){
			$.dialog.confirm('确定要删除吗？该文章将会永久删除！',function(){
				$.getJSON("/admin/article/del.do?rp.articleId="+article.id, function(data){
					if(data.result == 1){
						alert("删除成功");
						ArticleList.getData();
					} else {
						alert("删除失败");
					}
				});					
			},function(){});
		},
		view : function(){
		}
	};
	
	var ArticleList = {
		getData : function(){
			$.postJson({
				url: "/admin/article/list.do?t=" + new Date().getTime(),
				data: Parameter,
				success: function(data){
					ArticleList.render(data.pager.pageData);
					Pager.renderPager(data.pager);
				}
			});
		},
		render : function(pageData){
			var tbody = $("<tbody></tbody>");
			var article,tr,td;
			for(var i=0;i<pageData.length;i++){
				article = pageData[i];
				tr = $("<tr class='" + ((i+1)%2==0 ? "even" : "odd") + "'></tr>");
				tr.append("<td class='tc'>"+article.id+"</td>");
				tr.append("<td>"+article.title+"</td>");
				tr.append("<td>"+article.memo.substring(0,50)+" ...</td>");
				tr.append("<td class='tc'>"+article.categoryDesc+" - "+article.artTypeDesc+"</td>");
				tr.append("<td class='tc'>"+article.postTime+"</td>");
				tr.append("<td class='tc'>"+article.clickNum+"</td>");
				tr.append("<td class='tc'>"+article.indexDesc+"</td>");
				tr.append("<td class='tc'>"+article.statusDesc+"</td>");
				td = $("<td class='tc'></td>");
				td.append( $("<a class='icon icon-edit'></a>").click(function(art){
					return function(){
						Operation.modify(art);
					};
				}(article)));
				td.append($("<a class='icon icon-del'></a>").click(function(art){
					return function(){
						Operation.del(art);
					};
				}(article)));
				td.append("<a class='icon icon-add' target='_blank' href='/admin/article/preview.do?rp.articleId="+article.id+"'></a>");
				
				tbody.append(tr.append(td));
			}
			tableBody.empty().append(tbody.children());
			tbody = null;
		}
	};
	var Pager = {
		renderPager : function(pager){
		//log($("#pagination").pagination)
			$("#pagination").pagination({
				totalNum : pager.totalNum,
				totalPage : pager.totalPage,
				currentPage : pager.pageNo,
				displayNum : 6,
				edgeNum : 1,
				callback : function(pageNo){
				//log(pageNo)
					Parameter["rp.pageNo"] = pageNo ;
					ArticleList.getData();
				}
			});
		}
	};
	
	var ArticleQuery = {
			
			dataMap : {},
			
			initCategory : function(){
				$.getJSON("/admin/config/cg_listCategory.do", function(data){
					ArticleQuery.renderCategorySelect(data);
				});
			},
			initStatus : function(){
				var status = $("#articleStatus");
				status.append("<option value='-1'>状态不限</option>");
				$.getJSON("/comm/getArticleState.do", function(data){
					var state = data.articleState;
					for(var id in state){
						status.append("<option value='"+id+"'>"+state[id]+"</option>");
					}
				});
				status.change(function(){
					Parameter["rp.status"] = this.value;
				});
			},
			renderCategorySelect : function(data){
				var categorysData = data.categorys;
				var category = $("#articleCategory");
				category.append("<option value='-1'>大类不限</option>");
				for(var i=0;i<categorysData.length;i++){
					category.append("<option value='"+categorysData[i].id+"'>"+categorysData[i].name+"</option>");
					ArticleQuery.dataMap[categorysData[i].id] = categorysData[i].subCategory;
				}
				ArticleQuery.renderSubSelect(categorysData[0].id);
				category.change(function(){
					ArticleQuery.renderSubSelect($(this).val());
					Parameter["rp.categoryType"] = this.value;
				});
			},
			renderSubSelect : function(categoryId){
				var artType = $("#articleType");
				artType.empty();
				artType.append("<option value='-1'>小类不限</option>");
				if(categoryId == -1){
					return;
				}
				var subData = ArticleQuery.dataMap[categoryId];
				for(var i=0;i<subData.length;i++){
					artType.append("<option value='"+subData[i].id+"'>"+subData[i].name+"</option>");
				}	
				artType.change(function(){
					Parameter["rp.artcletype"] = this.value;
				});
			}
	};
	
	$(document).ready(function(){
		tableBody = $("#content #al-tbody");
		$("#aq-submit").click(function(){
			Parameter["rp.pageNo"] = 1;
			ArticleList.getData();
		});
		ArticleQuery.initCategory();
		ArticleQuery.initStatus();
		ArticleList.getData();
	});
})(jQuery);